package com.situ.feemanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.feemanagement.mapper.CategoryMapper;
import com.situ.feemanagement.pojo.entity.Category;
import com.situ.feemanagement.pojo.query.CategoryQuery;
import com.situ.feemanagement.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> selectAll() {
        return categoryMapper.selectAll();
    }

    @Override
    public Map<String, Object> selectByPage(CategoryQuery categoryQuery) {
        PageHelper.startPage(categoryQuery.getPage(), categoryQuery.getLimit());
        List<Category> list = categoryMapper.selectByPage(categoryQuery);
        PageInfo<Category> pageInfo = new PageInfo<Category>(list);
        int totalCount = (int) pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", totalCount);
        map.put("list", list);
        return map;
    }

    @Override
    public void deleteById(int id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            categoryMapper.deleteById(id);
        }
    }

    @Override
    public void add(Category category) {
        categoryMapper.add(category);
    }

    @Override
    public Category selectById(int id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public void update(Category category) {
        categoryMapper.update(category);
    }
}
